package com.jsbs.iam.ident.controller;

import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.ident.dto.ApplyConfigGetDto;
import com.jsbs.iam.ident.dto.ApplyTypeDeleteDto;
import com.jsbs.iam.ident.dto.UserInfoDeleteDto;
import com.jsbs.iam.ident.service.UserApiService;
import com.jsbs.iam.ident.vo.ApplyAccountVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @BelongsProject: iam
 * @BelongsPackage: com.jsbs.iam.ident.controller
 * @Author: WangJiYuan
 * @Description: 用于user远程调用ident
 * @Version: 2023-01-16  19:34
 */
@Slf4j
@RestController
@RequestMapping("/iam/ident/userApi")
public class UserApiController extends BaseController {

    @Autowired
    private UserApiService userApiService;

    /**
     * 删除账户数据, 登出
     * @param deleteDto
     * @return
     */
    @PostMapping("/deleteAccount")
    public Result deleteAccount(HttpServletRequest request, HttpServletResponse response, @RequestBody UserInfoDeleteDto deleteDto) {
        if(CollectionUtils.isEmpty(deleteDto.getUserCodes())){
            log.warn("Route=>UserApiController method=>deleteAccount 删除账户数据");
            return Result.fail("请确实勾选信息是否无误!");
        }
        try {
            //删除账户数据
            int count = userApiService.deleteAccount(deleteDto,request, response);
            log.info("Route=>UserApiController method=>deleteAccount 删除账户数据成功！{}",count);
            return Result.successWithMsg("删除" +count+ "条账户信息成功！");
        }catch (Exception e){
            log.error("Route=>UserApiController method=>deleteAccount 删除账户数据异常：{}", e.getMessage());
            return handleException(e,"账号删除异常!");
        }

    }

    /**
     * 删除应用类型关联的策略
     * @param deleteDto
     * @return
     */
    @PostMapping("/deleteStrategyRelation")
    public Result deleteStrategyRelation(@RequestBody ApplyTypeDeleteDto deleteDto) {
        if(CollectionUtils.isEmpty(deleteDto.getApplyTypeCodes())){
            log.warn("Route=>UserApiController method=>deleteStrategyRelation 删除应用类型关联的策略");
            return Result.fail("请确实勾选信息是否无误!");
        }
        try {
            //删除应用类型关联的策略
            userApiService.deleteStrategyRelation(deleteDto);
            log.info("Route=>UserApiController method=>deleteStrategyRelation 删除应用类型关联的策略成功！");
            return Result.successWithMsg("删除应用类型关联的策略成功");
        }catch (Exception e){
            log.error("Route=>UserApiController method=>deleteStrategyRelation 删除应用类型关联的策略异常：{}", e.getMessage());
            return handleException(e,"删除应用类型关联的策略异常!");
        }

    }

    /**
     * 根据用户数据查询账号数据
     * @param applyConfigGetDto
     * @return
     */
    @PostMapping("/getAccountDataByUser")
    public Result getAccountDataByUser(@RequestBody ApplyConfigGetDto applyConfigGetDto) {
        try {
            if (StringUtils.isEmpty(applyConfigGetDto.getCompanyCode())) {
                log.warn("Route=>UserApiController method=>getAccountDataByUser 根据用户数据查询账号数据，远程调用方登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(applyConfigGetDto.getCreatePerson())) {
                log.warn("Route=>UserApiController method=>getAccountDataByUser 根据用户数据查询账号数据，远程调用方登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            //根据用户数据查询账号数据
            List<ApplyAccountVo> accountDataVos = userApiService.getAccountDataByUser(applyConfigGetDto);

            log.info("Route=>UserApiController method=>getAccountDataByUser 根据用户数据查询账号数据,成功！");
            return Result.success("根据用户数据查询账号数据成功",accountDataVos);
        }catch (Exception e){
            log.error("Route=>UserApiController method=>getAccountDataByUser 根据用户数据查询账号数据,异常：{}", e.getMessage());
            return handleException(e,"删除应用类型关联的策略异常!");
        }

    }
}
